package SMUVE;

import java.util.Vector;


public class AdminInfo {
	
	ServerInterface server_interface;
	private String name;
	private Vector<Room> rooms;
	private Vector<Player> players;
	//private Vector<Item> items;
	public static final String delimiter = "|";
	
	public AdminInfo()
	{
	}

	public void set_server(ServerInterface s)
	{
		server_interface = s;
	}
	
	public String send_message(String msg)
	{
		String ret_msg = server_interface.admin_thread.send_message(msg);
		System.out.println("Received Message: " + ret_msg + " from server");
		String msg_split[] = msg.split("\\" + delimiter);
		int type = -1;
		if(msg_split[0].equals("get_players"))
		{
			type = 0;
		}
		else if(msg_split[0].equals("get_player"))
		{
			type = 1;
		}
		update(ret_msg, type);
		type = -1;
		return ret_msg;
	}
	public void update(String msg, int type)
	{
		/*Commands Taken:
		shutdown
		add_room
		add_portal
		get_portals
		add_player
		get_players
		get_room_players
		get_rooms
		get_player
		move*/

		String msg_split[] = msg.split("\\" + delimiter);
		String cmd = msg_split[0];
		if(cmd.equals("quit"))
		{
		}
		else if(cmd.equals("add_room_success"))
		{
			//@todo grab room data from array
			//need: name, description
			String name = msg_split[1];
			String desc = msg_split [2];
			Room r = new Room(name, desc);
			rooms.add(r);
			System.out.println("Room Added");
		}
		else if(cmd.equals("add_portal_success"))
		{
			//@todo r = source room of portal
			//need: reference to source room, 2nd room name
			Room r = new Room();
			r.add_portal("connected room name");
			System.out.println("Portal Added");
		}
		else if(cmd.equals("get_portals_success"))
		{
			System.out.print("Portals For Requested Room: ");
			for(int c = 1; c < msg_split.length; c++)
			{
				if(c == msg_split.length - 1)
					System.out.print(msg_split[c]);
				else
					System.out.print(msg_split[c] + " , ");
			}
			System.out.print("\n");
		}
		else if(cmd.equals("add_player_success"))
		{
			//@todo get player data from string
			//need: name, room_name
			String name = msg_split[1];
			String room_name = msg_split[2];
			Player p = new Player(name, room_name);
			players.add(p);
			System.out.println("Player Added");
		}
		else if(cmd.equals("get_player_success"))
		{
			if(msg_split.length > 2)
			{
				if(type == 0)
				{
					System.out.print("Players In World: ");
					for(int c = 1; c < msg_split.length; c++)
					{
						if(c == msg_split.length - 1)
							System.out.print(msg_split[c]);
						else
							System.out.print(msg_split[c] + " , ");
					}
					System.out.print("\n");
				}
				else
				{
					System.out.println("Requested Player: " + msg_split[1] + ", Room: " + msg_split[2]);
				}
			}
		}
		else if(cmd.equals("get_room_players_success"))
		{
			System.out.print("Players In Requested Room: ");
			for(int c = 1; c < msg_split.length; c++)
			{
				if(c == msg_split.length - 1)
					System.out.print(msg_split[c]);
				else
					System.out.print(msg_split[c] + " , ");
			}
			System.out.print("\n");
		}
		else if(cmd.equals("get_rooms_success"))
		{
			System.out.print("Rooms In World: ");
			for(int c = 1; c < msg_split.length; c++)
			{
				if(c == msg_split.length - 1)
					System.out.print(msg_split[c]);
				else
					System.out.print(msg_split[c] + " , ");
			}
			System.out.print("\n");
		}
		else if(cmd.equals("get_player_success"))
		{
			System.out.println("Requested Player: " + msg_split[1] + ", Room: " + msg_split[2]);
		}
		else if(cmd.equals("success"))
		{
			System.out.println("Move Successful");
		}
		else System.out.println("Invalid Command");
	}
}
